package com.pic.mgt.haishin.converter;

import java.util.Date;

import com.pic.mgt.haishin.unit.HaishinBusinessUnit;
import com.pic.mgt.monitor.BusinessUnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;
import com.pic.mgt.unit.Business;

/**
 * [配信確認檔內容處理]
 * 處理項目: * 
 * 1. 配信時間:確認檔內容的第 1行
 * 2. 檔案大小:確認檔內容的第2行
 * @author 洪政緯
 *
 */
public abstract class HaishinBusinessUnitConverter extends BusinessUnitConverter {
	private final int executionTimeLineNo = 1; // 啟動時日:確認檔內容的第 1 行
	private final int filesizeLineNo = 2;      // 檔案大小:確認檔內容的第 2 行
	
	private FormatParser executionTimeParser;
	private FormatParser filesizeParser;
	
	private HaishinBusinessUnit haishinBusinessUnit;
	
	@Override
	protected void parseIGXTypeContent(Business business)
							throws WrongUnitFormatException {
		this.haishinBusinessUnit = (HaishinBusinessUnit)business;		
		
		// 分析並填入配信內容 (第三層級)
		doFill();
		
		// 檢查 Target filesize
		// WrongUnitFormatException
		boolean matched = 
				super.checkTargetFilesizeMatched(
						this.haishinBusinessUnit.getBusinessFilesize());
		if (!matched) {
			throw new WrongUnitFormatException(
			"The specified filesize is not equal to the filesize of target file.");
		}
		
		
		// 分析並填入配信業務內容 (第四層級)
		parseBizTypeContent(this.haishinBusinessUnit);
	}
	
	/*
	 * 放入配信內容資訊:
	 * (1) 啟動時日
	 * (2) 業務檔案大小
	 */
	private void doFill() throws WrongUnitFormatException {
		// --------------------- (1) 啟動時日 ---------------------
		fillExecutionTime();
		
		// --------------------- (2)  業務檔案大小 ---------------------
		// throws WrongConfirmationContentFormatException
		fillFilesize();
	}
	
	private void fillExecutionTime() throws
									WrongUnitFormatException {
		try {
			// 取得執行時間字串
			// throw IndexOutOfBoundsException
			String executionTimeString = super.
						getConfirmationContentAtLine(this.executionTimeLineNo - 1);
			// 轉換執行時間字串為日期
			Date executionTime = (Date)
					executionTimeParser.parse(executionTimeString);
			this.haishinBusinessUnit.setExecutionTime(executionTime);
		} catch (IndexOutOfBoundsException e) {
			throw new WrongUnitFormatException(
					"Can't find execution time at line " +
					this.executionTimeLineNo, e);
		}
	}
	
	private void fillFilesize() throws WrongUnitFormatException {
		try {			
			// throws IndexOutOfBoundsException
			String filesizeStr = super.
					getConfirmationContentAtLine(this.filesizeLineNo - 1);
			Long filesize = (Long)this.filesizeParser.parse(filesizeStr);
			this.haishinBusinessUnit.setBusinessFilesize(filesize);
		} catch (IndexOutOfBoundsException e) {			
			throw new WrongUnitFormatException(
					"Can't find file size at line " + this.filesizeLineNo, e);
		}
	}
	
	abstract void parseBizTypeContent(HaishinBusinessUnit haishinBusinessUnit)
						throws WrongUnitFormatException;
	
	// --- getter and setter ---
	
	public void setExecutionTimeParser(FormatParser executionTimeParser) {
		this.executionTimeParser = executionTimeParser;
	}
	
	public void setFilesizeParser(FormatParser filesizeParser) {
		this.filesizeParser = filesizeParser;
	}
}
